package 
{
   import flash.display.DisplayObjectContainer;
   import flash.display.Loader;
   import flash.display.MovieClip;
   import flash.events.Event;
   import flash.events.ProgressEvent;
   import flash.net.URLRequest;
   
   import float.MyFloat;
   
   import jp.nium.display.getInstanceById;
   import jp.progression.commands.display.AddChild;
   import jp.progression.commands.display.RemoveChild;
   
   /**
    * @author cjboy1984@gmail.com
    * @usage
    * CMD.showRule(stage);
    */
   public class CMD
   {
      // singleton
      private static var _instance:CMD;
      
      // [rule]
      private const rule_id:String = 'rule main';
      private function get rule():MovieClip { return MovieClip(getInstanceById(rule_id)); }
      
      // [scene loading]
      private function get rootLoading_1():MovieClip { return MovieClip(getInstanceById('data loading 1')); }
      
      // loader
      private var target_id:String;
      private var loader:Loader;
      private var isLoading:Boolean;
      
      // --------------------- LINE ---------------------
      
      public function CMD(ptv:PrivateClass) {}
      
      // --------------------- LINE ---------------------
      
      public static function getInstance():CMD
      {
         if (!CMD._instance)
         {
            CMD._instance = new CMD(new PrivateClass());
            CMD._instance.isLoading = false;
         }
         
         return CMD._instance;
      }
      
      // ________________________________________________
      //                                             rule
      
      public static function showRule(container:DisplayObjectContainer):void
      {
         if (!CMD.getInstance().rule)
         {
            // loading
            CMD.getInstance().addLoading(container);
            
            // set id
            CMD.getInstance().target_id = CMD.getInstance().rule_id;
            
            // old
            CMD.getInstance().clearOldLoader();
            
            // new
            CMD.getInstance().isLoading = true;
            var rule_loader:Loader = new Loader();
            rule_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, CMD.getInstance().onDataLoading);
            rule_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, CMD.getInstance().onDataLoadComplete);
            rule_loader.load(new URLRequest('rule.swf'));
         }
         else
         {
            MyFloat.add(CMD.getInstance().rule_id);
         }
      }
      
      // --------------------- LINE ---------------------
      
      // ################### protected ##################
      
      // #################### private ###################
      
      private function clearOldLoader():void
      {
         if (loader)
         {
            loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onDataLoading);
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onDataLoadComplete);
            
            if (isLoading)
            {
               loader.unloadAndStop(true);
            }
            else
            {
               loader.unload();
            }
         }
      }
      
      private function onDataLoading(e:ProgressEvent):void
      {
         var perc:int = int(100 * e.bytesLoaded / e.bytesTotal);
//         rootLoading_1.perc = perc.toString() + ' %';
      }
      
      private function onDataLoadComplete(e:Event):void
      {
         isLoading = false;
         
         var loader:Loader = Loader(e.target.loader);
         loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onDataLoading);
         loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onDataLoadComplete);
         
         // loading
         removeLoading();
         
         MyFloat.add(target_id);
      }
      
      // --------------------- LINE ---------------------
      
      private function addLoading(container:DisplayObjectContainer):void
      {
         if (!container.contains(rootLoading_1)) 
         {
            new AddChild(container, rootLoading_1).execute();
         }
      }
      
      private function removeLoading():void
      {
         if (rootLoading_1.parent) 
         {
            new RemoveChild(rootLoading_1.parent, rootLoading_1).execute();
         }
      }
      
      // --------------------- LINE ---------------------
      
   }
   
}

class PrivateClass
{
   public function PrivateClass()
   {
      trace('Private class is up');
   }
}
